<script setup>
import { ref } from 'vue'
// import { getUserInfo, saveUserInfo } from '@/api/user'
import { ElMessage } from 'element-plus'
import { getAddress, addAddress, updateAddress, deleteAddress } from '@/api/adress'
import useUserStore from '@/store/modules/user'

// 获取登录用户信息
const userStore = useUserStore();
console.log(userStore.id);

const addresses = ref([
]); 

// 定义变量
const addDialog = ref(false)
const editDialog = ref(false)

// 定义添加变量2
const addForm = ref({})
// 定义编辑变量
const editForm = ref({})
const isDefault = ref([
  { label: '是', value: 'Y' },
  { label: '否', value: 'N' }
])

// 获取地址列表
function getAddressList() {
  // 请求接口
  getAddress(userStore.id).then(res => {
    console.log(res)
    addresses.value = res.data.data
  })
}
getAddressList();

// 添加地址按钮
function addAdress() {
    addDialog.value = true
}

// 添加新地址
function addNewAddress(data) {
    addAddress(data).then(res => {
    console.log(res)
    if (res.code === 200) {
      ElMessage.success('添加成功')
    }
  })
    addDialog.value = false
    getAddressList()
}


// 打开编辑
function editAdress(item) {
  editDialog.value = true
  editForm.value = item
}

// 编辑地址
function editAddress() {
  updateAddress(editForm.value).then(res => {
    console.log(res)
    if (res.code === 200) {
      ElMessage.success('编辑成功')
    }
  })
  editDialog.value = false
  getAddressList()
}

// 删除
function delAdress(addressid) {
    console.log(addressid)
    deleteAddress(addressid).then(res => {
        console.log(res)
        if (res.code === 200) {
            ElMessage.success('删除成功')
            getAddressList()
        }
  })
    editDialog.value = false
}



</script>

<template>
    <h2 style="padding-left: 30px;">地址列表</h2>
    <el-button type="primary" @click="addAdress" style="margin-left: 77%;">添加地址</el-button>
    <hr style="margin-bottom: 20px;">
    <div class="user-center">
        <div v-for="item in addresses" :key="item.addressid" class="address-item">
            <ul @click="editAdress(item)">
                <li v-if="item.isdefault==='Y'"><span style="color: #e58686;">*默认地址*</span></li>
                <li><span>收<i />货<i />人：</span>{{ item.name }} 
                    <span style="margin-left: 300px;">联系方式：</span>{{ item.phone }}
                </li>
                <li><span>收货地址：</span>{{ item.province }} {{ item.city }} {{ item.district }} {{ item.detail }}</li>
            </ul>
        </div>
        
    </div>
    
    <!-- 地址弹窗 -->
    <el-dialog v-model="editDialog " title="编辑地址" width="30%">
      <el-form :model="editForm" label-width="80px">
          <el-form-item label="收货人">
          <el-input v-model="editForm.name" placeholder="请输入收货人姓名"></el-input>
          </el-form-item>
          <el-form-item label="联系电话">
          <el-input v-model="editForm.phone" placeholder="请输入收货人联系电话"/>
          </el-form-item>
          <el-form-item label="收货地址">
            <el-input v-model="editForm.province" placeholder="请输入省份"/>
            <el-input style="margin-top: 3px;" v-model="editForm.city" placeholder="请输入市"/>
            <el-input style="margin-top: 3px;" v-model="editForm.district" placeholder="请输入区县"/>
            <el-input style="margin-top: 3px;" v-model="editForm.detail" placeholder="请输入详细地址"/>
          </el-form-item>
          <el-form-item label="默认地址">
            <el-cascader
              v-model="editForm.is_default"
              :options="isDefault"
              placeholder="是否设为默认地址"
              clearable
              style="width: 100%"
            />
          </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="delAdress">删除地址</el-button>
          <el-button type="primary" @click="editAddress">修改地址</el-button>
        </span>
      </template>
    </el-dialog>
    <el-dialog
      title="添加地址"
      v-model="addDialog"
      width="30%"
      :before-close="handleClose"
    >
    <el-form :model="addForm" label-width="80px">
          <el-form-item label="收货人">
          <el-input v-model="addForm.name" placeholder="请输入收货人姓名"></el-input>
          </el-form-item>
          <el-form-item label="联系电话">
          <el-input v-model="addForm.phone" placeholder="请输入收货人联系电话"/>
          </el-form-item>
          <el-form-item label="收货地址">
            <el-input v-model="addForm.province" placeholder="请输入省份"/>
            <el-input style="margin-top: 3px;" v-model="addForm.city" placeholder="请输入市"/>
            <el-input style="margin-top: 3px;" v-model="addForm.district" placeholder="请输入区县"/>
            <el-input style="margin-top: 3px;" v-model="addForm.detail" placeholder="请输入详细地址"/>
          </el-form-item>
          <el-form-item label="默认地址">
            <el-cascader
              v-model="addForm.is_default"
              :options="isDefault"
              placeholder="是否设为默认地址"
              clearable
              style="width: 100%"
            />
          </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="addDialog = false">取消</el-button>
          <el-button type="primary" @click="addNewAddress">确定添加</el-button>
        </span>
      </template>
    </el-dialog>
</template>

<style scoped lang="scss">
.user-center{
    width: 600px;
    margin-left: 80px;
}

.address-item{
    width: 800px;

    ul {
        display: flex;
        flex-direction: column;
        margin: 20px;
        padding: 10px;
        border-radius: 5px;
        box-shadow: 0 0 10px #ccc;
        color: #363636;
        font-size: 18px;
        background-color: #ffecec;
        cursor: pointer;


        li{
            display: flex;
            align-items: center;
        }
    }
}

</style>